package com.liu.network.socket;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Client4 {
	/**
	 * 十六进制转byte数组
	 */
	public static byte[] hexStringToByteArray(String hexStr) {
		if (hexStr.length() % 2 == 1) {
			hexStr = "0" + hexStr;
		}
		int len = hexStr.length() / 2;
		byte[] result = new byte[len];
		char[] chars = hexStr.toCharArray();
		for (int i = 0; i < len; i++) {
			result[i] = (byte) (charToByte(chars[i]) << 4 | charToByte(chars[i + 1]));
		}
		return result;
	}
	private static int charToByte(char c) {
		String chars = "0123456789ABCDEF";
		byte b = (byte) chars.indexOf(c);
		return b;
	}
	/**
	 * byte数组转十六进制
	 */
	public static String byteArrayToHexString(byte[] bArr) {
		StringBuffer sb = new StringBuffer(bArr.length);
		String sTmp;

		for (int i = 0; i < bArr.length; i++) {
			sTmp = Integer.toHexString(0xFF & bArr[i]);
			if (sTmp.length() < 2)
				sb.append(0);
			sb.append(sTmp.toUpperCase());
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		try {
			final Socket socket = new Socket("192.168.3.11", 56789);
			// 往服务器发送请求
			OutputStream os = socket.getOutputStream();
			byte[] head = new byte[8];
			byte[] requestFlag = hexStringToByteArray("464E4950");
			byte[] requestDateLen = new byte[4];
			requestDateLen = "0".getBytes();
			for (int i = 0; i < requestFlag.length; i++) {
				head[i] = requestFlag[i];
			}
			for (int i = 0; i < requestDateLen.length; i++) {
				head[i + 4] = requestDateLen[i];
			}
			os.write(head);
			os.flush();
			socket.shutdownOutput();

			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			new Thread(new Runnable() {
	            @Override
	            public void run() {
	            	// 接收服务器的响应
	    			byte[] bytes = new byte[4];
	    			int responseDateLen = 0;
	    			byte[] responseDate = null;
	    			try {
	    				InputStream is = socket.getInputStream();
	    				BufferedInputStream bis = new BufferedInputStream(is);
	    				DataInputStream dis = new DataInputStream(bis);
	    				String responseFlag = "0x";
	    				String reply = "";
		    			while (dis.available() != 0) {
		    				for (int i = 0; i < 4; i++) {
		    					bytes[i] = dis.readByte();
		    				}
		    				responseFlag += byteArrayToHexString(bytes);
		    				bytes = new byte[4];
		    				for (int i = 0; i < 4; i++) {
		    					bytes[i] = dis.readByte();
		    				}
		    				responseDateLen = Integer.valueOf(new String(bytes).trim());
		    				responseDate = new byte[responseDateLen];
		    				for (int i = 0; i < responseDateLen; i++) {
		    					responseDate[i] = dis.readByte();
		    				}
		    				reply = new String(responseDate);
		    			}
		    			System.out.println("服务器返回参数" + reply);
		    			// 关闭资源
		    			dis.close();
		    			bis.close();
		    			is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    			
	            }
	        }).start();
			
//			switch (responseFlag) {
//			// 自定义规则
//			case "0x464E4949":
//
//				break;
//			// 黑白名单
//			case "0x464E4951":
//
//				break;
//			}

//			os.close();
//			socket.close();
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}